/*
 * @LastEditors: biz
 */
import { debounce, once, throttle } from "./sectionFn";
import { isObject } from "../utils/util";

import initPage from "./initPage";
import pageInitFn from "./pageInitFn";
import userFn from "./userFn";
import utilFn from "./utilFn";
import wxmlFn from "./wxmlFn";
import AniCanvas from "./canvas";

const defaultMixins = [pageInitFn, userFn, utilFn, { AniCanvas }, wxmlFn, initPage];
const originProperties = ['data', 'properties', 'options'];
const originMethods = [
    'onLoad', 'onShow', 'onReady', 'onHide', 'onUnload',
    'onPullDownRefresh', 'onReachBottom', 'onShareAppMessage',
    'onShareTimeline', 'onAddToFavorites', 'onPageScroll',
    'onResize', 'onTabItemTap'
];

// 主要的合并函数，将页面选项和mixin合并
export function setMerge(options, use = []) {
    if (!Array.isArray(options.mixins)) options.mixins = [];
    options.mixins.push(...defaultMixins, ...use);
    const mixins = options.mixins;
    delete options.mixins;
    return mergeMixins(mixins, options);
}

// 对mixin数组和页面选项进行合并
function mergeMixins(mixins, options) {
    mixins.forEach(mixin => {
        validateMixin(mixin);
        Object.entries(mixin).forEach(([key, value]) => {
            if (originProperties.includes(key)) {
                options[key] = Object.assign({}, options[key], value);
            } else if (originMethods.includes(key)) {
                const originFunc = options[key];
                if(key==='onShareAppMessage'){
                    options[key] =async function(...args) {
                        const res=await originFunc.apply(this, args);
                        console.log(res)
                        if(res){
                            return res
                        }
                        console.log('【ANI INFO】当前执行了mixins中的onShareAppMessage')
                        return value.apply(this, args);
                    }
                }else{
                    options[key] = function(...args) {
                        value.apply(this, args);
                        return originFunc && originFunc.apply(this, args);
                    }
                }

            } else {
                if (!options[key]) {
                    options[key] = value;
                }
            }
        });
    });
    applyFunctionModifiers(options);
    return options;
}

// 验证mixin是否为合法对象
function validateMixin(mixin) {
    if (Object.prototype.toString.call(mixin) !== '[object Object]') {
        throw new Error('Mixin should be an object');
    }
}

// 应用函数节流、防抖和单次执行的修饰符
function applyFunctionModifiers(options) {
    const modifiers = { throttle, debounce, once };
    Object.entries(modifiers).forEach(([modifierName, modifierFn]) => {
        if (options[modifierName] && isObject(options[modifierName])) {
            const config = options[modifierName];
            const time = config.time ||1000;
            Object.entries(config).forEach(([key, func]) => {
                if (typeof func === 'function') {
                    options[key] = modifierFn(func, time);
                }
            });
            delete options[modifierName]; // 清理原始配置
        }
    });
}

